import { axiosInstance } from "@/apis/request";

const ADMIN_UUID = "59c574cd-1a20-4314-b836-b0171eb0cf18";

const createUser = (
  name: string,
  password: string,
  region: any,
  isExpert: boolean = false,
  status: string = "1",
  company: string = "",
  legal: string = "",
  phone: string = "",
) => {
  const userData = {
    name,
    region,
    password,
    is_expert: isExpert,
    status,
    company,
    legal,
    phone,
  };
  return axiosInstance.post("evaluation/add_user/", userData);
};

const getAllFactors = () => {
  const userData = { uuid: ADMIN_UUID };
  return axiosInstance.post("evaluation/get_all_factor/", userData);
};

// const changeFactorValue = (keyName: string, value: number) => {
//   let factorData: any = {
//     factors: {},
//   };
//   factorData.factors[keyName] = value;
//   return axiosInstance.post("estimation/change_factors/", factorData);
// };

const changeFactorValue = (changes: any) => {
  let factorData: any = {
    factors: {},
  };
  factorData.factors = changes;
  return axiosInstance.post("evaluation/change_factors/", factorData);
};

const suggest = (message: string, uuid: string) => {
  const suggestData = { message, uuid };
  return axiosInstance.post("evaluation/suggest/", suggestData);
};

const getAllSuggestions = () => {
  const userData = { uuid: ADMIN_UUID };
  return axiosInstance.post(`evaluation/get_all_suggest/`, userData);
};

const getAllUnregisteredUsers = () => {
  const userData = { uuid: ADMIN_UUID };
  return axiosInstance.post(`evaluation/get_all_unregistered_user/`, userData);
};

const registerUserConfirm = (uuid: string) => {
  const userData = { uuid };
  return axiosInstance.post(`evaluation/confirm_enterprise_user/`, userData);
};

/**
 *
 * @param {string} userId
 * @param {string} postId
 * @param {string} message
 */
const addComment = (userId: string, postId: string, message: string) => {
  const commentData = { p_id: postId, u_id: userId, comment: message };
  return axiosInstance.post(`evaluation/add_comment/`, commentData);
};

const changeUserInfo = (
  userId: string,
  name: string,
  password: string,
  phone: string,
  region: any,
) => {
  const userData = { uuid: userId, password, name, phone, region };
  return axiosInstance.post(`evaluation/change_user_info/`, userData);
};

const getUserInfoById = (uuid: string) => {
  const idData = {
    uuid: uuid,
  };
  return axiosInstance.post(`evaluation/get_user_info/`, idData);
};

const getEvaluationResultById = (eva_code: string, option: string) => {
  const idData = { uuid: eva_code, option: option };
  return axiosInstance.post(`evaluation/get_result_list/`, idData);
};

const getResultById = (eva_code: string) => {
  const idData = { eva_code: eva_code };
  return axiosInstance.post(`evaluation/get_result/`, idData);
};

const getCommentListById = (eva_code: string) => {
  const idData = {
    p_id: eva_code,
  };
  return axiosInstance.post(`evaluation/get_post_all_comment/`, idData);
};

const evaluationFromRawData = (
  rawData: any,
  uuid: string,
  name: string = "测试数据",
) => {
  function groupByKey(objects: any[]) {
    const grouped: any = {};
    // console.log(objects);

    // 遍历列表中的每个对象
    objects.forEach((obj) => {
      Object.keys(obj).forEach((key) => {
        // 如果这个键名在grouped对象中不存在，则初始化一个数组
        if (!grouped[key]) {
          grouped[key] = [];
        }
        // 将当前对象的值添加到对应的数组中
        grouped[key].push(obj[key]);
      });
    });
    console.log(grouped);
    return grouped;
  }

  // console.log(groupByKey(rawData));
  let dataToBeEvaluate = groupByKey(rawData);
  return axiosInstance.post(`evaluation/evaluate/`, {
    uuid: uuid,
    name,
    data: dataToBeEvaluate,
  });
};

const getAllUsers = () => {
  const userData = { uuid: ADMIN_UUID };
  return axiosInstance.post(`evaluation/get_all_user/`, userData);
};

const getAllEvaluationId = () => {
  const userData = { uuid: ADMIN_UUID, option: "all" };
  return axiosInstance.post(`evaluation/get_result_list/`, userData);
};

const getRegionEvaluationId = (
  uuid: string = "be4efa5b-6d4c-4884-b68a-fe43a3f51aa7",
) => {
  const userData = { uuid, option: "region" };
  return axiosInstance.post(`evaluation/get_result_list/`, userData);
};

const userLoginWithPhoneAndPwd = (phone: string, password: string) => {
  const userData = { phone, password };
  return axiosInstance.post(`evaluation/user_login/`, userData);
};

export {
  createUser,
  getAllSuggestions,
  getEvaluationResultById,
  getAllUsers,
  suggest,
  getAllFactors,
  changeFactorValue,
  getAllUnregisteredUsers,
  addComment,
  registerUserConfirm,
  changeUserInfo,
  getAllEvaluationId,
  getCommentListById,
  evaluationFromRawData,
  getRegionEvaluationId,
  userLoginWithPhoneAndPwd,
  getUserInfoById,
  getResultById,
};
